package com.demo.controller.admin;

import java.util.Date;
import java.util.List;

import com.demo.common.model.Article;
import com.demo.common.model.ArticleCategory;
import com.demo.common.model.Category;
import com.demo.common.model.Tag;
import com.demo.controller.BaseController;
import com.jfinal.core.Controller;

public class CategoryAdminController extends BaseController<Category>{
	
	public void index(){
		renderJson(Category.me.paginate(getParaToInt("page"), 10 ,getQueryParam()));
	}
	public void getById(){
		setAttr("article", Article.me.findById(getParaToInt()));
		render("/article.html");
	}
	
	//添加
	public void saveOrUpdate(){
		Category cate  = new Category();
		Boolean bool = false;
		String id = getPara("id");
		cate.set("name", getPara("name"))
						   .set("description", getPara("description"))
						   .set("sort", getPara("sort"))
						   .set("create_date", new Date());
		if(id!=null&&!id.equals("")){
			bool = cate.set("id", id).update();
		}else{
			bool = cate.set("status", 1).save();
		}
		if (bool) {
			success("添加成功");
		} else {
			error("添加失败");
		}
		
	}
//	//更新操作
//	public void update() {
//		Category cate  = new Category();
//		Boolean bool = cate.set("id", getPara("id"))
//				.set("name", getPara("name"))
//				.set("sort", getPara("sort"))
//				.set("description", getPara("description"))
//				.set("create_date", new Date()).update();
//		if (bool) {
//			success("更新成功");
//		} else {
//			error("更新失败");
//		}
//	}
	//跳转编辑页面
	public void toEdit() {
		String id = getPara("id");
		// Tag.me.findById(id);
		setAttr("cate", Category.me.findById(id));
		render("/admin/article/category-edit.html");
	}
	//是否禁用
	public void operate() {
		String type = getPara("type");
		Boolean b;
		if ("yes".equals(type)) {
			b = true;
		} else {
			b = false;
		}
		Category cate  = new Category();
		Boolean bool = cate.set("id", getPara("id")).set("status", b).update();
		if (bool) {
			success("操作成功");
		} else {
			error("操作失败");
		}
	}
	
	// 批量删除
	public void batchDel() {
		String[] ids = getPara("ids").split(",");
		Boolean bool = false;
		if (ids != null && ids.length > 0) {
			for (int i = 0; i < ids.length; i++) {
				 List<ArticleCategory> aclist = ArticleCategory.me.getAllById("category_id",ids[i]);
				 for(ArticleCategory ac:aclist){
					 bool = ac.delete();
				 }
				bool = Category.me.deleteById(ids[i]);
			}

		}
		if (bool) {
			success("操作成功");
		} else {
			error("操作失败");
		}
	}
	
	

}
